package com.wei.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.entity.Daily;
import com.wei.feign.MemberClientFeign;
import com.wei.service.DailyService;
import com.wei.mapper.DailyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HASEE
 * @description 针对表【statistics_daily(网站统计日数据)】的数据库操作Service实现
 * @createDate 2022-12-05 04:08:01
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily>
        implements DailyService {
    @Autowired
    private MemberClientFeign memberClientFeign;

    @Override
    public void createStatistics(String day) {

        /*
            数据库中只存储一条某一天的统计数量
              方法1：更新那条记录
              方法2：删除之前记录，重新加入新的
         */

        //删除几小时前生成的数据
        LambdaQueryWrapper<Daily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Daily::getDateCalculated, day);

        baseMapper.delete(queryWrapper);

        //从ucenter表中获取新的数据
        Integer countRegister = memberClientFeign.countRegister(day);

        Daily statisticsDaily = new Daily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(countRegister);

        //随机数
        statisticsDaily.setLoginNum(RandomUtil.randomInt(100, 300));
        statisticsDaily.setVideoViewNum(RandomUtil.randomInt(100, 300));
        statisticsDaily.setCourseNum(RandomUtil.randomInt(10, 200));

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> createChartData(String type, String begin, String end) {

        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("date_calculated", type).between("date_calculated", begin, end);

        List<Daily> statisticsDailyList = baseMapper.selectList(queryWrapper);

        //因为返回有两部分数据：日期 和 日期对应的数量
        //前端要求数组json结构，对应后端java代码的是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> dateList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        for (Daily statisticsDaily : statisticsDailyList) {

            //向日期集合中添加数据
            String dateCalculated = statisticsDaily.getDateCalculated();
            dateList.add(dateCalculated);

            //向数量集合中添加数据
            switch (type) {
                case "register_num": {
                    countList.add(statisticsDaily.getRegisterNum());
                    break;
                }
                case "login_num": {
                    countList.add(statisticsDaily.getLoginNum());
                    break;
                }
                case "video_view_num": {
                    countList.add(statisticsDaily.getVideoViewNum());
                    break;
                }
                case "course_num": {
                    countList.add(statisticsDaily.getCourseNum());
                    break;
                }
                default:
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("countList", countList);

        return map;
    }
}




